<template>
  <div>
    <el-dialog
      v-loading="loading"
      class="mt-dialog"
      :title="title"
      :visible.sync="dialogVisible"
      top="80px"
      width="1280px"
      @close="close"
    >
      <el-input
        v-model="ps_product_id"
        placeholder="请输入网站的产品ID"
        class="input-with-select"
        clearable
        @keyup.native.enter="handleSearch"
      >
        <el-button slot="append" icon="el-icon-search" :loading="getloading" :disabled="loading" @click="handleSearch">获取feed产品信息
        </el-button>
      </el-input>

      <div :style="showinfo">
        <el-tabs v-model="activeName" @tab-click="handleModifyTabClick">
          <el-tab-pane label="常用信息" name="first">
            <el-form ref="currentOpRow" :model="currentOpRow" label-width="80px">
              <el-form-item label="link">
                <el-input v-model="currentOpRow.link" />
              </el-form-item>
              <el-form-item label="title">
                <el-input v-model="currentOpRow.title" />
              </el-form-item>
              <el-form-item label="price">
                <el-input v-model="currentOpRow.price.value" />
              </el-form-item>
              <el-form-item label="短描述">
                <el-input v-model="currentOpRow.description" type="textarea" />
              </el-form-item>
              <el-form-item label="图片链接">
                <el-input v-model="currentOpRow.imageLink" />
                <img :src="currentOpRow.imageLink" class="image" width="200px" height="200px">
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="自定义标签" name="second">
            <el-form ref="currentOpRow" :model="currentOpRow" label-width="300px">
              <el-form-item v-if="currentOpRow.customLabel0 !== undefined" label="customLabel0[订单量]">
                <el-input v-model="currentOpRow.customLabel0" />
              </el-form-item>
              <el-form-item v-if="currentOpRow.customLabel1 !== undefined" label="customLabel1[自定义]">
                <el-autocomplete
                  v-model="currentOpRow.customLabel1"
                  style="width: 90%"
                  clearable
                  class="inline-input"
                  :fetch-suggestions="querySearch"
                  placeholder="请输入命令"
                />
              </el-form-item>
              <el-form-item v-if="currentOpRow.customLabel2 !== undefined" label="customLabel2[产品关键词]">
                <el-input v-model="currentOpRow.customLabe2" />
              </el-form-item>
              <el-form-item v-if="currentOpRow.customLabel3 !== undefined" label="customLabel3[未使用]">
                <el-input v-model="currentOpRow.customLabe3" disabled />
              </el-form-item>
              <el-form-item v-if="currentOpRow.customLabel4 !== undefined" label="customLabel4[未使用]">
                <el-input v-model="currentOpRow.customLabe4" disabled />
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="其他信息" name="three">
            <el-form ref="currentOpRow" :model="currentOpRow" label-width="160px">
              <el-form-item label="gender">
                <el-input v-model="currentOpRow.gender" />
              </el-form-item>

              <el-form-item label="targetCountry">
                <el-input v-model="currentOpRow.targetCountry" type="textarea" />
              </el-form-item>
            </el-form>
          </el-tab-pane>

        </el-tabs>
      </div>
      <span slot="footer" class="dialog-footer" :style="showinfo">
        <el-tag>这里是通过api修改了单个产品,如果feed定期更新，请重新生成feed文件</el-tag>
        <el-button type="danger" @click="handleDeleteProduct">删除Feed产品</el-button>
        <el-button type="primary" @click="handleSaveModifyRemote(true)">立即修改</el-button>
        <a target="_blank" :href="currentOpRow.link" style="float: left">打开产品</a>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { GetFeedProduct, UpdateFeedProduct, DeleteFeedProduct } from '@/api/orders/merchantfeed'
import { Get as GetWebsite } from '@/api/erp/elicink/website'

export default {
  name: 'Feedproduct',
  props: ['pstVisible', 'website_id', 'product_id'],
  data() {
    return {
      title: '获取/修改feed产品',
      activeName: 'first',
      getloading: false,
      dialogVisible: false,
      loading: false,
      ps_product_id: 'online:en:US:',
      showinfo: 'display:none;',
      searchResults: [],
      searchImageUrl: '',
      restaurants: [],
      currentOpRow: {
        'id': 0,
        'name': '',
        'price': '',
        'description': '',
        'imageLink': '',
        'link': ''
      }
    }
  },
  watch: {
    product_id(v) {
      if (v) {
        this.ps_product_id = 'online:en:US:' + v
      }
    },
    pstVisible(v) {
      this.dialogVisible = v
      if (v) {
        this.hanldleGetWebsite()
        if (this.ps_product_id !== 'online:en:US:') {
          this.handleSearch()
        }
      }
    }
  },
  mounted() {
    this.restaurants = this.loadAll()
  },
  methods: {
    hanldleGetWebsite() {
      GetWebsite(this.website_id, null)
        .then(response => {
          this.title = response.data.domain + '获取/修改Googel Shopping 产品'
        }).catch(err => {
          console.log(err)
        })
    },
    querySearch(queryString, cb) {
      var restaurants = this.restaurants
      var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter(queryString) {
      return (restaurant) => {
        return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
      }
    },
    loadAll() {
      return [
        { 'value': '7d_no_order' },
        { 'value': '15d_no_order' },
        { 'value': '30d_no_order' },
        { 'value': 'remove' },
        { 'value': 'hot' },
        { 'value': '15d_hot' }
      ]
    },
    handleDeleteProduct() {
      this.$prompt('请输入删除原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        this.delete_loading = true
        DeleteFeedProduct({
          ps_product_id: this.ps_product_id,
          url: this.currentOpRow.link,
          image: this.currentOpRow.imageLink,
          reason: value,
          website_id: this.website_id
        })
          .then(response => {
            this.$notify({
              title: '成功',
              message: response.message,
              type: 'success',
              duration: 2000
            })
            this.delete_loading = false
          }).catch(err => {
            this.$notify({
              title: '失败',
              message: err,
              type: 'success',
              duration: 2000
            })
            this.delete_loading = false
          })
      }).catch(() => {
      })
    },
    handleSaveModifyRemote(isRemote) {
      this.$prompt('请输入修改原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        this.modify_loading = true
        UpdateFeedProduct({
          'ps_product_id': this.ps_product_id,
          website_id: this.website_id,
          price: this.currentOpRow.price.value,
          title: this.currentOpRow.title,
          link: this.currentOpRow.link,
          reason: value,
          description: this.currentOpRow.description,
          imageLink: this.currentOpRow.imageLink,
          customLabel0: this.currentOpRow.customLabel0,
          customLabel1: this.currentOpRow.customLabel1,
          customLabel2: this.currentOpRow.customLabel2,
          customLabel3: this.currentOpRow.customLabel3,
          customLabel4: this.currentOpRow.customLabel4
        })
          .then(response => {
            this.$notify({
              title: '成功',
              message: response.msg,
              type: 'success',
              duration: 2000
            })
            this.modify_loading = false
          }).catch(err => {
            this.$notify({
              title: '失败',
              message: err,
              type: 'success',
              duration: 2000
            })
            this.modify_loading = false
          })
      }).catch(() => {
      })
    },
    handleModifyTabClick() {
      console.log('tab click')
    },
    handleSearch() {
      if (this.ps_product_id === 'online:en:US:') {
        alert('请填入产品ID')
        return
      }
      this.showinfo = 'display:none;'
      this.getloading = true
      this.currentOpRow = {
        'id': 0,
        'name': '',
        'price': '',
        'description': '',
        'imageLink': '',
        'link': ''
      }
      GetFeedProduct({ 'website_id': this.website_id, 'ps_product_id': this.ps_product_id }).then(respone => {
        this.currentOpRow = respone.data
        this.getloading = false
        this.showinfo = 'display:inline;'
      }).catch(e => {
        this.getloading = false
      })
    },
    close() {
      this.title = '请核对域名信息，如果域名不对请不要提交'
      this.$emit('closePstDialog', false)
    }
  }
}
</script>

<style scoped>

  .top >>> .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

  .input-with-select {
    width: calc(100% - 200px);
  }

  .app-container >>> .el-dialog {
    margin-top: 48px !important;
    margin-bottom: 5px;
  }

  .mt-dialog >>> .el-transfer-panel {
    width: calc(50% - 60px);
  }

  .mt-form >>> .el-select {
    width: 100%;
  }

  .mt-form >>> .el-input__inner {
    width: 100%;
  }

  .mt-dialog >>> .el-dialog {
    margin: 10px auto !important;
  }

  .mt-dialog >>> .el-dialog__body {
    padding-bottom: 60px;
    padding-top: 5px;
  }

  .top >>> .el-select {
    width: 300px;
  }
</style>
